package com.gx.smart.construction.ui.common.page.adapter;


import android.widget.Filter;

import com.gx.wisestone.constructionappgrpclib.grpc.organizationalstructure.ConstructionAppOrganizationalStructureDto;

import java.util.ArrayList;
import java.util.List;


/**
 * @author: xiaosy
 * 时间: 2021/11/19
 * @Description: 房间列表过滤器
 * @Version: 1.0
 **/
public class RoomListFilter extends Filter {
    private RoomListAdapter adapter;
    private List<ConstructionAppOrganizationalStructureDto> filterList = new ArrayList<>();
    private List<ConstructionAppOrganizationalStructureDto> sourceList = new ArrayList<>();


    public RoomListFilter(RoomListAdapter adapter) {
        this.adapter = adapter;
        this.sourceList.addAll(adapter.getListData());
    }

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        FilterResults results = new FilterResults();
        filterList.clear();
        if (constraint != null && constraint.length() != 0) {
            List<ConstructionAppOrganizationalStructureDto> list = sourceList;
            for (ConstructionAppOrganizationalStructureDto item : list) {
                if (item.getNumber().contains(constraint)) {
                    filterList.add(item);
                }
            }
        } else {
            filterList.addAll(sourceList);
        }
        results.values = filterList;
        results.count = filterList.size();
        return results;
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        adapter.refresh((List<ConstructionAppOrganizationalStructureDto>)results.values);
    }
}
